package cn.jhc.db;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.h2.jdbcx.JdbcDataSource;
import cn.jhc.utils.Constants;

/**
 * 锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟洁，锟斤拷始锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷菘锟�
 *
 */
@WebListener
public class DbInitLister implements ServletContextListener {

    /**
     * Default constructor. 
     */
    public DbInitLister() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent event)  { 
    	ServletContext context = event.getServletContext();
    	String realPath = context.getRealPath("/webstore.db");
    	JdbcDataSource dataSource = new JdbcDataSource();
    	dataSource.setUrl("jdbc:h2:"+realPath);
    	dataSource.setUser("sa");
    	dataSource.setPassword("");
    	context.setAttribute(Constants.H2_DATASOURCE, dataSource);
        MyDbutils.Createtable(dataSource);
    	
	}

	

	
    public void contextDestroyed(ServletContextEvent arg0)  { 
         // TODO Auto-generated method stub
    }
	
}
